<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻详情</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: auto;
            overflow: hidden;
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }

        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }

        nav a:hover {
            background: #444;
        }

        .news-article {
            background: #fff;
            margin: 20px 0;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .news-article img {
            max-width: 100%;
            height: auto;
            display: block;
            margin-bottom: 20px;
        }

        .news-article h1 {
            margin: 0;
            padding-bottom: 10px;
        }

        .news-article p {
            color: #666;
            line-height: 1.6;
            margin-bottom: 20px;
        }

        .news-article .meta {
            color: #888;
            font-size: 0.9em;
            margin-bottom: 20px;
        }

        .comments {
            background: #fff;
            margin-top: 20px;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .comment {
            border-bottom: 1px solid #eee;
            padding: 10px 0;
        }

        .comment:last-child {
            border-bottom: none;
        }

        .comment .author {
            font-weight: bold;
        }

        .comment .date {
            color: #888;
            font-size: 0.9em;
        }

        .comment-form {
            margin-top: 20px;
        }

        .comment-form label {
            display: block;
            margin-bottom: 5px;
        }

        .comment-form input[type="text"],
        .comment-form textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
        }

        .comment-form input[type="button"] {
            padding: 10px 20px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        .comment-form input[type="button"]:hover {
            background: #444;
        }

        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <header>
        <h1>新闻详情</h1>
    </header>
    <nav>
        <a href="index.html">返回首页</a>
    </nav>
    <div class="container">
        <div class="news-article">
            <!-- <img class="new_img"  src="https://via.placeholder.com/1200x630" alt="新闻图片">
            <h1 class="title">新闻标题</h1>
            <p class="meta">发布作者：<span class="newauther">admin</span></p>
            <p class="meta">发布时间：<span class="newtime"></span></p>
            <p class="newcontent">这里是新闻的详细内容，通常会包含新闻的全文，包括引言、正文和结论。</p> -->
        </div>
        <div class="comments">

        </div>
        <div class="comment-form">
            <h2>发表评论</h2>
            <form>
                <label for="comment">评论：</label>
                <textarea id="comment" name="comment" required></textarea>
                <input type="button" class="buildreview" value="提交评论">
            </form>
        </div>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    getNews()
    getReview()
    function getNews() {
        // 获得浏览器地址栏的参数
        let nid = window.location.search.substr(window.location.search.indexOf("nid")).split("=")[1];
        $.ajax({
            url: "http://www.dlrb.com:8082/news_detail.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                nid: nid
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    let context = `
                        <img class="new_img" width】="200" src="${resp.data.newImg}" alt="新闻图片">
                        <h1 class="title">${resp.data.title}</h1>
                        <p class="meta">发布作者：<span class="newauther">${resp.data.username}</span></p>
                        <p class="meta">发布时间：<span class="newtime">${resp.data.time}</span></p>
                        <p class="newcontent">${resp.data.context}</p>
                        `
                    $(".news-article").html(context)
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }

    function getReview() {
        // 获得浏览器地址栏的参数
        let nid = window.location.search.substr(window.location.search.indexOf("nid")).split("=")[1];
        $.ajax({
            url: "http://www.dlrb.com:8082/getReview.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                nid: nid
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                   let context = "";
                   for(let item of resp.data) {
                    context += `
                        <div class="comment">
                            <div class="comment-header">
                                <span class="comment-author">${item.username}</span> |
                                <span class="comment-date">${item.time}</span>
                            </div>
                            <hr>
                            <div class="comment-content">
                                ${item.context}
                            </div>
                        </div>
                    `;
                   }
                   $(".comments").html(context);
                } else {
                    console.log(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }

    $(".buildreview").click(function () {
        // 获得浏览器地址栏的参数
        let nid = window.location.search.substr(window.location.search.indexOf("nid")).split("=")[1];
        // 获得浏览器的cookie值
        let username = cookieToJson(document.cookie).username

        $.ajax({
            url: "http://www.dlrb.com:8082/buildReview.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                nid: nid,
                username: username,
                context:$("#comment").val()
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    alert(resp.msg)
                    $("#comment").val("")
                    getReview()
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })

    })

    function cookieToJson(cookieString) {
        // 处理空字符串
        if (!cookieString || typeof cookieString !== 'string') {
            return {};
        }
        // 分割 Cookie 字符串
        const cookies = cookieString.split(';');
        const result = {};
        // 遍历每个 Cookie
        for (const cookie of cookies) {
            // 跳过空字符串
            if (!cookie.trim()) {
                continue;
            }
            // 查找第一个等号位置
            const equalsIndex = cookie.indexOf('=');

            // 如果没有等号，跳过这个 Cookie
            if (equalsIndex === -1) {
                continue;
            }
            // 提取键和值
            const name = cookie.substring(0, equalsIndex).trim();
            let value = cookie.substring(equalsIndex + 1).trim();
            // 处理可能的 URL 编码
            try {
                value = decodeURIComponent(value);
            } catch (e) {
                // 如果解码失败，使用原始值
                console.warn(`Failed to decode cookie value: ${value}`, e);
            }
            // 添加到结果对象
            result[name] = value;
        }
        return result;
    }


</script>

</html>